<!-- <template>
  <div class="background" style="width: 100%;height: 100vh;background-color: #9c0f47;overflow: hidden">
    <div style="width: 400px;margin: 150px auto">
    <div style="color: #e2ffda;font-size: 30px;text-align: center;padding: 30px 0"> 欢迎注册</div>
      <el-form :model="form" size="normal" ref="form">
        <el-form-item label="昵称" prop="username">
          <el-input  v-model="form.username">

          </el-input>

        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password/>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm" >
          <el-input v-model="form.confirm" show-password/>
        </el-form-item>
        <el-form-item >
          <el-button style="width: 100%;"  type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script >
import  request  from '@/utils/request';

export default {
  name: "Register",
  data() {
    return {
      form: {},
      rules: {
        username: [
          {required: true, message: '请输入昵称', trigger: 'blur'},

        ],
        name: [
          {required: true, message: '请输入您的真实姓名', trigger: 'blur'},

        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},

        ],
        confirm: [
          {required: true, message: '请确认密码', trigger: 'blur'},

        ]
      }
    }
  },
  methods: {
    register() {

      if(this.form.username==null){
        this.$message({
            type:"error",
          message: "请输入昵称"

      })
        return;
      }

      if(this.form.name==null){
        this.$message({
          type:"error",
          message: "请输入姓名"

        })
        return;
      }
      if (this.form.password != this.form.confirm) {
        this.$message(
            {
              type: "error",
              message: "请检查2次密码输入是否一致"
            }
        )
        return
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/register", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功",


              })
              this.$router.push("/login")
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }

          })

        }
      })
    }
  }
}

</script>

<style scoped>
.background{
width: 150px;

height: 60px;

background-image: url(https://foruda.gitee.com/images/1694788408132124808/84d92aa5_10348758.jpeg );

background-size: cover
}
</style> -->

<template>
  <div class="background" style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #669fef">
    <div style="display: flex; background-color: white; width: 40%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="https://foruda.gitee.com/images/1694788447935557289/81006ed1_10348758.jpeg" alt="" style="width: 75%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; ">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" style="width: 100%" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer" @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>

  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    // 验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        confirmPass: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
      }
    }
  },
  created() {

  },
  methods: {
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/register', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

.background{
width: 100%;

height: 60px;

background-image: url(https://foruda.gitee.com/images/1694788408132124808/84d92aa5_10348758.jpeg );

background-size: cover

}

</style>